package com.imooc.mall.service.impl;

import com.imooc.mall.common.ResultInfo;
import com.imooc.mall.common.ResultInfoUtil;
import com.imooc.mall.dao.CategoryMapper;
import com.imooc.mall.dto.CategoryDto;
import com.imooc.mall.pojo.Category;
import com.imooc.mall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResultInfo<List<CategoryDto>> findAll() {
        /**
         * 一次将所有类目查出来
         */
        List<CategoryDto> categoryDtoList=new ArrayList<>();
        List<Category> categoryList = categoryMapper.findAll();
      /*  for (Category category : categoryList) {
            if (category.getParentId()==0){
                CategoryDto categoryDto=new CategoryDto();
                BeanUtils.copyProperties(category,categoryDto);
                categoryDtoList.add(categoryDto);
            }
        }*/
        categoryDtoList = categoryList.stream().
                filter(x -> x.getParentId() == 0).
                map(e -> categoryToCategoryDto(e)).
                sorted(Comparator.comparing(CategoryDto::getSortOrder).reversed()).
                collect(Collectors.toList());
        /**
         * 查询子目录
         */
        findSubCategory(categoryDtoList,categoryList);

        return ResultInfoUtil.success(categoryDtoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categoryList = categoryMapper.findAll();
        findSubCategoryId(id, resultSet, categoryList);
    }

    private void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categoryList) {
        for (Category category : categoryList) {
            if (category.getParentId().equals(id)) {
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(),resultSet,categoryList);
            }

        }
    }


    private void findSubCategory(List<CategoryDto> categoryDtoList, List<Category> categoryList) {
        for (CategoryDto categoryDto : categoryDtoList) {
            List<CategoryDto> subCategoryDtoList=new ArrayList<>();//子类目集合
            for (Category category : categoryList) {
                //最开始用==比较报错，源码中，说cache中已有-128到127，不在这范围的会新new ，这时可以理解比较是内存地址，也就是是不是同一对象.
                //所以说当Integer的值不在-128到127的时候使用==方法判断是否相等就会出错
                /**
                 * 如果查到内容，设置子目录subCategory，继续往下查
                 */
                if (categoryDto.getId().equals(category.getParentId())){
                    //如果最外层的id等于数据库查出类目的parentId，说明该数据库类目就是对应的子目录
                    CategoryDto subCategory = categoryToCategoryDto(category);
                    subCategoryDtoList.add(subCategory);//该数据库类目也就是子目录加入集合
                }

            }
            //根据sort_order进行排序，默认是升序，需求是降序，reversed()
            subCategoryDtoList.sort(Comparator.comparing(CategoryDto::getSortOrder).reversed());
            categoryDto.setSubCategories(subCategoryDtoList);//加入外层
            findSubCategory(subCategoryDtoList,categoryList);//继续调用方法，现在的最外层变为上面的第二层，对第二层进行往下查找
        }

    }

    private CategoryDto categoryToCategoryDto(Category category){
        CategoryDto categoryDto=new CategoryDto();
        BeanUtils.copyProperties(category,categoryDto);
        return categoryDto;
    }

}
